#include "../src/dulinklist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 声明底层链表的函数库 */
extern func_DLinkList fun_DLinkList;

typedef struct __value
{
    DLinkListNode node;
    int data;
} value_t;

int main(int argc, const char *argv[])
{
    int i; 

    value_t* pv = NULL;
    value_t v1, v2, v3, v4, v5;

    v1.data = 1;
    v2.data = 2;
    v3.data = 3;
    v4.data = 4;
    v5.data = 5;

    DLinkList *dlist = fun_DLinkList.create();

    fun_DLinkList.insert(dlist, (DLinkListNode *)&v1, fun_DLinkList.length(dlist));
    fun_DLinkList.insert(dlist, (DLinkListNode *)&v2, fun_DLinkList.length(dlist));
    fun_DLinkList.insert(dlist, (DLinkListNode *)&v3, fun_DLinkList.length(dlist));
    fun_DLinkList.insert(dlist, (DLinkListNode *)&v4, fun_DLinkList.length(dlist));
    fun_DLinkList.insert(dlist, (DLinkListNode *)&v5, fun_DLinkList.length(dlist));

    printf("\ndata : ");
    for (i = 0; i < fun_DLinkList.length(dlist); i++)
    {
        pv = (value_t *)fun_DLinkList.get(dlist, i);
        printf("%d\t", pv->data);
    }
    printf("\n\n");

    pv = ( value_t *)fun_DLinkList.delete(dlist, fun_DLinkList.length(dlist) - 1);
    printf("delete : %d\n", pv->data);
    pv = ( value_t *)fun_DLinkList.delete(dlist, 0);
    printf("delete : %d\n\n", pv->data);
    
    printf("data : ");
    for (i = 0; i < fun_DLinkList.length(dlist); i++)
    {
        pv = ( value_t *)fun_DLinkList.next(dlist);
        printf("%d\t", pv->data);
    }
    printf("\n\n");

    fun_DLinkList.reset(dlist);
    fun_DLinkList.next(dlist);
    fun_DLinkList.next(dlist);
    pv = ( value_t *)fun_DLinkList.current(dlist);
    printf("next current : %d\n", pv->data);
    fun_DLinkList.prev(dlist);
    pv = ( value_t *)fun_DLinkList.current(dlist);
    printf("prev current : %d\n", pv->data);


    fun_DLinkList.reset(dlist);
    fun_DLinkList.next(dlist);
    pv = ( value_t *)fun_DLinkList.current(dlist);
    printf("current : %d\n", pv->data);

    printf("length : %d\n", fun_DLinkList.length(dlist));

    fun_DLinkList.clear(dlist);
    fun_DLinkList.destroy(&dlist);

    if(dlist == NULL) 
    {
        printf("free success\n");
    }    
    else
    {
        free(dlist);
        printf("free failed!\n");
    }
    
    return 0;
}

